<?php

namespace StudyBuddy\Math;

use \StudyBuddy\Math\MathBase;
use \StudyBuddy\Math\ExpressionMath;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Math
 */
class Math {

    //put your code here
    public $dirimg = '/w/img/equation';

    public function __construct($dirimg='') {
        if ($dirimg)
            $this->dirimg = $dirimg;
    }

//    
//    public function Math($dirimg='') {
//        if($dirimg) 
//            $this->dirimg = $dirimg; 
//    }

    public function detectimg($n) {
        /*
          Detects if the formula image already exists in the $dirimg cache directory.
          In that case, the function returns a parameter (recorded in the name of the image file) which allows to align correctly the image with the text.
         */
        $ret = 0;
        $handle = opendir($_SERVER['DOCUMENT_ROOT'] . $this->dirimg);
        while ($fi = readdir($handle)) {
            $info = pathinfo($fi);
            if ($fi != "." && $fi != ".." && $info["extension"] == "png" && preg_match("/^math/", $fi)) {
                list($math, $v, $name) = explode("_", $fi);
                if ($name == $n) {
                    $ret = $v;
                    break;
                }
            }
        }
        closedir($handle);
        return $ret;
    }

    public function mathimage($text, $size, $pathtoimg) {
        /*
          Creates the formula image (if the image is not in the cache) and returns the <img src=...></img> html code.
         */
        $nameimg = md5(trim($text) . $size) . '.png';
        $v = self::detectimg($nameimg);
        if ($v == 0) {
            //the image doesn't exist in the cache directory. we create it.
            $math_base = new MathBase();
            $formula = new ExpressionMath($math_base->tableau_expression(trim($text)));
            $formula->dessine($size);
            $v = 1000 - imagesy($formula->image) + $formula->base_verticale + 3;
            //1000+baseline ($v) is recorded in the name of the image
            ImagePNG($formula->image, $_SERVER['DOCUMENT_ROOT'] . $this->dirimg . "/math_" . $v . "_" . $nameimg);
        }
        $valign = $v - 1000;
        return '<img src="' . $pathtoimg . "math_" . $v . "_" . $nameimg . '" style="vertical-align:' . $valign . 'px;' . ' display: inline-block ;" alt="' . $text . '" title="' . $text . '"/>';
    }

    public function mathfilter($text, $size, $pathtoimg) {
        /* THE MAIN FUNCTION
          1) the content of the math tags (<m></m>) are extracted in the $t variable (you can replace <m></m> by your own tag).
          2) the "mathimage" function replaces the $t code by <img src=...></img> according to this method :
          - if the image corresponding to the formula doesn't exist in the $dirimg cache directory (detectimg($nameimg)=0), the script creates the image and returns the "<img src=...></img>" code.
          - otherwise, the script returns only the <img src=...></img>" code.
          To align correctly the formula image with the text, the "valign" parameter of the image is required.
          That's why a parameter (1000+valign) is recorded in the name of the image file (the "detectimg" function returns this parameter if the image exists in the cache directory)
          To be sure that the name of the image file is unique and to allow the script to retrieve the valign parameter without re-creating the image, the syntax of the image filename is :
          math_(1000+valign)_md5(formulatext.size).png.
          (1000+valign is used instead of valign directly to avoid a negative number)
         */
        $text = stripslashes($text);
        $size = max($size, 10);
        $size = min($size, 24);
        preg_match_all("|<m>(.*?)</m>|", $text, $regs, PREG_SET_ORDER);
        foreach ($regs as $math) {
            $t = str_replace('<m>', '', $math[0]);
            $t = str_replace('</m>', '', $t);
            $code = self::mathimage(trim($t), $size, $pathtoimg);
            $text = str_replace($math[0], $code, $text);
        }
        return $text;
    }

}

?>
